#include <algorithm>
#include <vector>
#include <string>
#include <iostream>

// 定义一个书籍的结构体，包含书籍的ID、标题和价格
struct Book {
    int id;
    std::string title;
    double price;
};

int main() {
    // 初始化一些书籍
    std::vector<Book> books = {
        {1, "C++ Primer", 45.95},
        {2, "Effective Modern C++", 54.99},
        {3, "The C++ Programming Language", 59.95}
    };

    // 要搜索的目标字符串
    std::string target = "C++";

    // 使用 Lambda 按值捕获 target，并使用列表初始化在 Lambda 内部创建新变量 v
    auto count_by_value = [&books, v = target]() {
        return std::count_if(books.begin(), books.end(), [v](const Book& book) {
            return book.title.find(v) != std::string::npos;
        });
    };

    // 使用 Lambda 按引用捕获 target，并使用列表初始化在 Lambda 内部创建新变量 r
    auto count_by_reference = [&books, &r = target]() {
        return std::count_if(books.begin(), books.end(), [&r](const Book& book) {
            return book.title.find(r) != std::string::npos;
        });
    };

    // 输出按值捕获和按引用捕获的计数结果
    std::cout << "Count by value: " << count_by_value() << std::endl;
    std::cout << "Count by reference: " << count_by_reference() << std::endl;

    return 0;
}